package com.ymm.driver.core.facade;

import com.ymm.driver.api.constants.Dto.CargoDto;
import com.ymm.driver.api.constants.Dto.DriverDto;
import com.ymm.driver.api.constants.Dto.DriverGrabDto;
import com.ymm.driver.api.constants.Entity.DrbillEntity;
import com.ymm.driver.api.constants.Entity.DriverEntity;
import com.ymm.driver.api.facade.DriverPersonalAllInformationService;
import com.ymm.driver.api.utils.Result;
import com.ymm.driver.api.utils.ResultCodeEnum;
import com.ymm.driver.dao.mapper.DriverPersonalAllInformationMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Company: CUG
 * @Description: TODO
 * @Author: LiYangyong
 * @Date: 2022/8/3/003 10:28
 **/

@Component
@Service(version = "server")
public class DriverPersonalAllInformationServiceImpl implements DriverPersonalAllInformationService {


    @Resource
    DriverPersonalAllInformationMapper driverPersonalAllInformationMapper;

    /**
     * 忘记密码后修改密码实现
     *
     * @param driverPhone
     * @param driverPassword
     */
    @Override
    public void driverChangePassword(String driverPhone, String driverPassword) {

        driverPersonalAllInformationMapper.updateDriverPasswordByPhone(driverPhone, driverPassword);
        return;
    }

    /**
     * 司机注册接口实现
     *
     * @param driverPhone
     * @param driverPassword
     * @return
     */
    @Override
    public void driverRegisterService(String driverPhone, String driverPassword) {
        driverPersonalAllInformationMapper.addDriverById(driverPhone, driverPassword);
        return;
    }

    /**
     * 通过电话查询司机用户
     *
     * @param driverPhone
     * @return
     */
    @Override
    public String queryDriverByPhone(String driverPhone) {
        return driverPersonalAllInformationMapper.selectDriverByPhone(driverPhone);
    }


    /**
     * 司机登录
     *
     * @param driverPhone
     * @return
     */
//    @Override
//    public String driverLoginService(String driverPhone) {
//        return driverPersonalAllInformationMapper.selectDriverPasswordByPhone(driverPhone);
//    }
    @Override
    public DriverEntity driverLoginService(String driverPhone) {
        return driverPersonalAllInformationMapper.selectDriverPasswordByPhone(driverPhone);
    }


    @Override
    public String selectDriverPasswordByPhone(String driverPhone) {
        return null;
    }


    /**
     * 司机认证
     *
     * @param driverPhone
     * @param driverIdNum
     * @param driverName
     * @param driverCarType
     * @param driverCarNum
     */
    @Override
    public void driverAuthentication(String driverPhone, String driverIdNum, String driverName, Integer driverCarType, String driverCarNum, Integer driverSex) {
        driverPersonalAllInformationMapper.driverAuthentication(driverPhone, driverIdNum, driverName, driverCarType, driverCarNum, driverSex);
    }

    /**
     * 司机身份认证
     *
     * @param driverPhone
     * @param driverName
     * @param driverIdNum
     * @param driverSex
     */
    @Override
    public void driverInfoAuthentication(String driverPhone, String driverName, String driverIdNum, Integer driverSex) {

        driverPersonalAllInformationMapper.driverInfoAuthentication(driverPhone, driverName, driverIdNum, driverSex);
        return;
    }

    /**
     * 车型、车牌认证
     *
     * @param driverPhone
     * @param driverCarType
     * @param driverCarNum
     */
    @Override
    public void carInfoAuthentication(String driverPhone, Integer driverCarType, String driverCarNum) {

        driverPersonalAllInformationMapper.carInfoAuthentication(driverPhone, driverCarType, driverCarNum);
        return;
    }


    /**
     * 个人那中心
     */
    @Override
    public void driverPersonalCenter() {

    }

    /**
     * 点击编辑进入个人信息页面
     *
     * @param driverId
     * @return
     */
    @Override
    public DriverDto driverEditorPersonInformation(BigInteger driverId) {
        return driverPersonalAllInformationMapper.selectDriverDtoById(driverId);
    }


    /**
     * 点击提交，修改编辑的信息
     *
     * @param driverId
     * @param driverPhone
     * @param driverCarType
     * @param driverCarNum
     */
    @Override
    public void submitInformation(BigInteger driverId, String driverPhone, Integer driverCarType, String driverCarNum) {
        driverPersonalAllInformationMapper.submitInformation(driverId, driverPhone, driverCarType, driverCarNum);
        return;
    }

    /**
     * 开通会员
     */
    @Override
    public void driverJoinVIP(BigInteger driverId) {
        driverPersonalAllInformationMapper.driverJoinVIP(driverId, 1);
        return;
    }

    /**
     * 查询限现在司机的会员到期时间
     *
     * @param driverId
     * @return
     */
    @Override
    public DriverEntity queryNowVipTime(BigInteger driverId) {
        return driverPersonalAllInformationMapper.queryNowVipTime(driverId);
    }

    /**
     * 司机会员到期时间
     *
     * @param driverId
     * @return
     */
    @Override
    public void driverVipTime(BigInteger driverId, Date driverVipTime) {
        driverPersonalAllInformationMapper.driverVipTime(driverId, driverVipTime);
        return;
    }


    /**
     * 进入钱包
     *
     * @param driverId
     * @return
     */
    @Override
    public BigDecimal driverWallet(BigInteger driverId) {
        return driverPersonalAllInformationMapper.driverWallet(driverId);
    }

    /**
     * 联系客服
     */
    @Override
    public void driverFeedBack() {

    }


    @Override
    public void grabOrder(DriverGrabDto driverGrabDto) {

        return;
    }


    /**
     * 根据司机ID查看司机是否是会员
     *
     * @param driverId
     * @return
     */
    @Override
    public int selectDriverTypeByDriverId(BigInteger driverId) {
        return driverPersonalAllInformationMapper.selectDriverTypeByDriverId(driverId);
    }





    /**
     * @Author: chencong
     * @Description: 根据司机ID查询司机信息
     * @DateTime: 11:50 2022/8/7
     * @Params: [driverId]
     * @Return Result
     */

    @Override
    public Result<DriverEntity> driverSearchInfo(BigInteger driverId) {
        DriverEntity driverEntity = driverPersonalAllInformationMapper.getDriverDetailsById(driverId);

        if (driverEntity == null) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        return Result.success(driverEntity);

    }

    /**
     * @Author: chencong
     * @Description: 根据司机ID列表查询司机信息
     * @DateTime: 11:50 2022/8/7
     * @Params: [driverIds]
     * @Return Result<List < DriverEntity>>
     */

    @Override
    public Result<List<DriverEntity>> batchSearchDriverInfo(List<BigInteger> driverIds) {
        List<DriverEntity> driverEntityList = new ArrayList();
        for (BigInteger id : driverIds) {
            driverEntityList.add(driverSearchInfo(id).getData());
        }
        if (driverEntityList.isEmpty()) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        return Result.success(driverEntityList);

    }

    @Override
    public Result getDriverProfitById(BigInteger driverId) {
        return Result.success(driverPersonalAllInformationMapper.getDriverProfitById(driverId));
    }


    /**
     * 普通充值流水
     *
     * @param driverId
     * @param drbillPrice
     * @param drbillType
     */
    @Override
    public void drbillNormalAddPrice(BigInteger driverId, Double drbillPrice, Integer drbillType, String drbillDescription) {
        driverPersonalAllInformationMapper.drbillNormalAddPrice(driverId, drbillPrice, drbillType, drbillDescription);
        return;
    }



    /**
     * 充值
     * @param driverId
     * @param driverProfit
     * @return
     */
    @Override
    public Result addDriverProfit(BigInteger driverId, Double driverProfit) {
        driverPersonalAllInformationMapper.addDriverProfit(driverId, driverProfit);
        return Result.success(driverPersonalAllInformationMapper.getDriverProfitById(driverId));

    }


    /**
     * 普通提现流水
     * @param driverId
     * @param drbillPrice
     * @param drbillType
     * @param drbillDescription
     */
    @Override
    public void drbillNormalSubtractPrice(BigInteger driverId, Double drbillPrice, Integer drbillType, String drbillDescription) {
        driverPersonalAllInformationMapper.drbillNormalSubtractPrice(driverId, drbillPrice, drbillType, drbillDescription);
        return;
    }

    /**
     * 司机收款
     * @param driverId
     * @param drbillPrice
     * @param drbillType
     * @param drbillDescription
     */
    @Override
    public void drbillCollectPrice(BigInteger driverId, Double drbillPrice, Integer drbillType, String drbillDescription) {

        driverPersonalAllInformationMapper.drbillCollectPrice(driverId, drbillPrice, drbillType, drbillDescription);
        return;
    }

    @Override
    public void drbillCollectSKPrice(BigInteger driverId, Double drbillPrice, Integer drbillType, String drbillDescription, BigInteger orderId) {
        driverPersonalAllInformationMapper.drbillCollectSKPrice(driverId, drbillPrice, drbillType, drbillDescription, orderId);
        return;
    }

    /**
     * 展示司机账单
     * 异步线程接口实现
     * @param driverId
     * @return
     */
    @Override
//    @Async("asyncExecutor")
    public List<DrbillEntity> showDrbill(BigInteger driverId) {
        List<DrbillEntity> drbillEntities = driverPersonalAllInformationMapper.showDrbill(driverId);
        return drbillEntities;
    }


    /**
     * 提现
     *
     * @param driverId
     * @param mount
     * @return
     */
    @Override
    public Result subtractDriverProfit(BigInteger driverId, Double mount) {
        Double accountMoney = driverPersonalAllInformationMapper.getDriverProfitById(driverId);
        if (accountMoney.compareTo(mount) == -1) {
            return Result.fail("余额不足，无法提现");
        }
        driverPersonalAllInformationMapper.subtractDriverProfit(driverId, mount);
        return Result.success(driverPersonalAllInformationMapper.getDriverProfitById(driverId));

    }

    @Override
    public void test(String driverPhone, String driverPassword) {
        driverPersonalAllInformationMapper.test(driverPhone, driverPassword);
        return;
    }

}

